package com.lemon.smartsafetynew.ui.adapter.hidden;

import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.lemon.smartsafety.R;
import com.lemon.smartsafety.app.AppService;
import com.lemon.smartsafety.app.IApplication;
import com.lemon.smartsafety.utils.common.LemonFileHelper;
import com.lemon.smartsafety.utils.common.StringUtils;
import com.lemon.smartsafety.utils.photo.GlideImageLoader;
import com.lemon.smartsafetynew.entity.event.hidden.HiddenMediaNewEvent;
import com.lemon.smartsafetynew.entity.hidden.SafeHiddenDangerFile;
import com.lemon.smartsafetynew.util.common.UrlFormatUtils;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import cn.finalteam.galleryfinal.widget.GFImageView;
import kale.adapter.LemonRcvAdapter;
import kale.adapter.item.AdapterItem;

/**
 * Created by Andrew on 2017/11/13.
 */

public class SafeHiddenAttachmentFileVoAdapter extends LemonRcvAdapter<SafeHiddenDangerFile> {

    private String tag;

    public SafeHiddenAttachmentFileVoAdapter(@Nullable List<SafeHiddenDangerFile> data,
                                             String tag) {
        super(data);
        this.tag = tag;
    }

    @NonNull
    @Override
    public AdapterItem createItem(Object type) {
        return new ItemAdapter();
    }

    public class ItemAdapter implements AdapterItem<SafeHiddenDangerFile> {

        private int mPosition;

        @Bind(R.id.ivPhoto)
        GFImageView ivPhoto;
        @Bind(R.id.ivDelete)
        ImageView ivDelete;
        @Bind(R.id.ripple)
        LinearLayout ripple;

        @Override
        public int getLayoutResId() {
            return R.layout.item_evidence_photo;
        }

        @Override
        public void bindViews(View root) {
            ButterKnife.bind(this, root);
            ivDelete.setVisibility(View.GONE);
        }

        @Override
        public void setViews() {
            ripple.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    HiddenMediaNewEvent hiddenMediaNewEvent = new HiddenMediaNewEvent(mDataList, mPosition, tag);
                    AppService.getBus().post(hiddenMediaNewEvent);
                }
            });
        }

        @Override
        public void handleData(SafeHiddenDangerFile safeHiddenDangerFile, int position) {

            mPosition = position;

            try {
                if (LemonFileHelper.isVoiceUrl(safeHiddenDangerFile.getPath())) {
                    ivPhoto.setImageDrawable(ContextCompat.getDrawable(IApplication.context(), R.mipmap.icon_new_voice));
                } else if (LemonFileHelper.isImageUrl(safeHiddenDangerFile.getPath())) {
                    if (StringUtils.nullValue(safeHiddenDangerFile.getPath()).length() != 0) {
                        GlideImageLoader.loadImage(ivPhoto.getContext(), UrlFormatUtils.formatFiles(safeHiddenDangerFile.getPath()), ivPhoto);
                    }
                } else {
                    ivPhoto.setVisibility(View.GONE);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }

        }
    }
}
